package com.utils.SM;

import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.StandardCharsets;

public class SM3Utils {

    /**
     * SM3签名
     *
     * @param data 签名数据
     * @return HexString
     */
    public static String sign(byte[] data) {
        SM3Digest sm3Digest = new SM3Digest();
        sm3Digest.update(data, 0, data.length);
        byte[] signBytes = new byte[sm3Digest.getDigestSize()];
        sm3Digest.doFinal(signBytes, 0);
        return Hex.toHexString(signBytes);
    }

    /**
     * SM3签名
     *
     * @param dataStr 签名数据
     * @return HexString
     */
    public static String sign(String dataStr) {
        byte[] data = dataStr.getBytes(StandardCharsets.UTF_8);
        return sign(data);
    }

    /**
     * SM3验证
     *
     * @param signData 已签名数据
     * @param dataStr 原数据
     * @return boolean
     */
    public static boolean verify(String signData, String dataStr) {
        byte[] data = dataStr.getBytes(StandardCharsets.UTF_8);
        return sign(data).equals(signData);
    }


}
